﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace MapControl.Misc
{
    public class BasicSquareCoord : SquareCoord
    {
        private SquareCoord bigSquare;
        public BasicSquareCoord(RectangleF rect, int colNum, int rowNum, SquareCoord parent)
            : base(rect, colNum, rowNum, parent)
        {
            bigSquare = parent;
        }

        /// <summary>
        /// Draw Basic BorderSquare
        /// </summary>
        /// <param name="g"></param>
        /// <param name="width"></param>
        public override void Draw(Graphics g, float width)
        {
            Pen pen = new Pen(Color.Blue, width);
            int totalX = NumOfColumns * bigSquare.NumOfColumns;
            int totalY = NumOfRows * bigSquare.NumOfRows;
            //Vẽ các trục giữa
            for (int i = 1; i < totalX; i++)
                g.DrawLine(pen, bigSquare.BorderSquare.X + Width * i, bigSquare.BorderSquare.Y, bigSquare.BorderSquare.X + Width * i, bigSquare.BorderSquare.Y + bigSquare.BorderSquare.Size.Height);

            //Vẽ các trục ngang
            for (int i = 1; i < totalY; i++)
                g.DrawLine(pen, bigSquare.BorderSquare.X, bigSquare.BorderSquare.Y + Height * i, bigSquare.BorderSquare.X + bigSquare.BorderSquare.Size.Width, bigSquare.BorderSquare.Y + Height * i);

            //Vẽ chữ số
            int index = -1;

            SolidBrush myBrush = new SolidBrush(Color.Red);
            Font myFont = new Font("Times New Roman", 14);
            //Vẽ chữ số ở trục X
            for (int j = 0; j < 5; j++)
            {
                float y = bigSquare.BorderSquare.Y + NumOfRows * Height * j;
                for (int i = 0; i < totalX; i++)
                {
                    index++;
                    //Reset
                    if (index == 10)
                        index = 0;

                    float x = bigSquare.BorderSquare.X + Width * i;
                    RectangleF rect1 = new RectangleF(x, y, Width, Height);
                    g.DrawString(index.ToString(), myFont, myBrush, rect1);
                }
            }

            index = -1;
            //Vẽ chữ số ở trục Y
            float x1 = bigSquare.BorderSquare.X + Width * NumOfColumns;
            for (int j = 0; j < totalY; j++)
            {
                index++;
                //Reset
                if (index == 10)
                    index = 0;
                if (index == 0)
                    continue;

                float y = bigSquare.BorderSquare.Y + Height * j;
                RectangleF rect1 = new RectangleF(x1, y, Width, Height);
                g.DrawString(index.ToString(), myFont, myBrush, rect1);

            }
            pen.Dispose();
        }

        /// <summary>
        /// Xác định tọa độ basic square
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public override string GetCoord(PointF p)
        {
            string value = "";
            int X, Y;
            X = (int)((p.X - parentSquareCoord.currentSquare.X) / Width);
            Y = (int)((p.Y - parentSquareCoord.currentSquare.Y) / Height);

            //Lưu thông tin tọa độ ô vuông hiện tại
            currentSquare = new RectangleF(parentSquareCoord.currentSquare.X + X * Width, parentSquareCoord.currentSquare.Y + Y * Height, Width, Height);

            //value = string.Format("({0},{1})", X.ToString(), Y.ToString());
            value = string.Format("{0}{1}", X.ToString(), Y.ToString());
            return value;
        }

    }
}
